/*
 * Unix SMB/CIFS implementation.
 *
 * Copyright (C) 2021-2022 Andreas Schneider <asn@samba.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>

#include <talloc.h>

#include "lib/replace/replace.h"
#include "lib/util/discard.h"
#include "lib/util/genrand.h"
#include "lib/util/data_blob.h"
#include "lib/util/talloc_stack.h"
#include "lib/crypto/gnutls_helpers.h"
#include "librpc/rpc/dcerpc_samr.h"

#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>

#include "lib/crypto/gnutls_aead_aes_256_cbc_hmac_sha512.c"

/* The following hexdumps are from a Windows Server 2022 time trace */
static const uint8_t session_key[] = {
	0x96, 0x17, 0x39, 0x9c, 0xa7, 0x54, 0x9e, 0x41,
	0xc4, 0x79, 0x71, 0x4b, 0xa0, 0x89, 0x5b, 0x0a
};

static const uint8_t salt_data[] = {
	0xac, 0x9c, 0xad, 0xcb, 0x66, 0xed, 0x2d, 0x05,
	0x55, 0x13, 0x18, 0xa9, 0xa5, 0x6b, 0xf9, 0x6f
};

static const uint8_t plaintext_data[] = {
	0x14, 0x00, 0x50, 0x00, 0x61, 0x00, 0x24, 0x00,
	0x24, 0x00, 0x77, 0x00, 0x30, 0x00, 0x72, 0x00,
	0x64, 0x00, 0x40, 0x00, 0x32, 0x00, 0xc2, 0x34,
	0x7d, 0x21, 0x79, 0x05, 0xef, 0x88, 0xd7, 0x11,
	0xec, 0xe2, 0xce, 0xb5, 0xd4, 0x4d, 0x64, 0x2d,
	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d0, 0xd7, 0x70, 0xad, 0xcd, 0x84, 0xbc, 0x1e,
	0x48, 0xa0, 0x20, 0x14, 0x10, 0xa4, 0xb1, 0x5b,
	0x05, 0x36, 0x9a, 0x6d, 0xb0, 0x10, 0x98, 0xbd,
	0x8d, 0xa2, 0xd1, 0xb2, 0xfa, 0x23, 0x37, 0xeb,
	0xb0, 0x04, 0x53, 0xcb, 0xa1, 0xa9, 0xc4, 0x88,
	0xdd, 0xf9, 0x80, 0xf5, 0xa9, 0xcd, 0x7b, 0xf8,
	0x77, 0x0b, 0x19, 0x84, 0x4c, 0xef, 0x2c, 0x14,
	0xa1, 0xdc, 0x9f, 0x2f, 0x41, 0xd0, 0xd0, 0x33,
	0x29, 0x8a, 0xb9, 0x39, 0x7e, 0xc9, 0x7f, 0xe7,
	0x63, 0x64, 0xa4, 0x7b, 0x4a, 0x6a, 0x33, 0xa7,
	0xaa, 0xf6, 0xca, 0x98, 0xc4, 0x9b, 0x62, 0x5b,
	0xcd, 0x53, 0x82, 0xbf, 0xf0, 0x0b, 0x9c, 0xe7,
	0xb2, 0x44, 0x1b, 0x88, 0x71, 0x61, 0xa1, 0x36,
	0x9e, 0x7a, 0x0a, 0x3c, 0x20, 0xd8, 0xff, 0xa1,
	0x23, 0x66
};

static const uint8_t expected_enc_key[] = {
	0xd9, 0xb2, 0x8b, 0xa1, 0x15, 0x74, 0xf6, 0x5a,
	0x73, 0xea, 0x82, 0xba, 0x99, 0x37, 0x54, 0x25,
	0x1b, 0x27, 0x20, 0xaa, 0xa3, 0xf7, 0xd5, 0x23,
	0x8f, 0x02, 0xe6, 0xe7, 0x21, 0x5b, 0xd2, 0xa9
};

static const uint8_t expected_mac_key[] = {
	0x8a, 0x04, 0x46, 0x6d, 0x5e, 0xe6, 0x2d, 0xb8,
	0x32, 0x9e, 0xab, 0xbe, 0xa4, 0x8b, 0x3f, 0x6c,
	0x3c, 0x1c, 0xa1, 0xaa, 0xb8, 0xec, 0x9c, 0x43,
	0xbc, 0x4b, 0x91, 0x35, 0x6f, 0x3a, 0xc4, 0xe2,
	0x62, 0x9b, 0xe8, 0x12, 0x63, 0x73, 0x94, 0x2a,
	0x59, 0x47, 0xfc, 0xd8, 0x78, 0x5d, 0x6d, 0x68,
	0x1b, 0x9f, 0x35, 0x31, 0x90, 0x27, 0xc8, 0xab,
	0x88, 0x8d, 0x80, 0xad, 0x7b, 0xea, 0xcd, 0xf5
};

static const uint8_t expected_auth_tag[] = {
	0x0a, 0x7a, 0xaf, 0x9e, 0xc1, 0x6e, 0x03, 0x12,
	0x51, 0x76, 0x04, 0xb2, 0x01, 0x17, 0xeb, 0x04,
	0xf3, 0xdd, 0xe3, 0xa7, 0x90, 0xfd, 0xc0, 0xca,
	0x96, 0x31, 0xbf, 0x30, 0x9e, 0xc5, 0x05, 0x5a,
	0xbc, 0xa8, 0xc1, 0xba, 0x56, 0x8f, 0x97, 0x5a,
	0x4e, 0x2a, 0x14, 0x0e, 0x4b, 0xf3, 0x6d, 0x9a,
	0x2a, 0x6e, 0xc3, 0x5e, 0x9d, 0x51, 0x0a, 0xf6,
	0xb2, 0x1b, 0xee, 0xe7, 0xf3, 0x09, 0x30, 0xc9,
};

static void torture_enc_key(void **state)
{
	DATA_BLOB key = data_blob_const(session_key, sizeof(session_key));
	uint8_t enc_key[32] = {0};
	NTSTATUS status;

	status = calculate_enc_key(&key, &samr_aes256_enc_key_salt, enc_key);
	assert_true(NT_STATUS_IS_OK(status));

	assert_memory_equal(expected_enc_key, enc_key, sizeof(enc_key));
}

static void torture_mac_key(void **state)
{
	DATA_BLOB key = data_blob_const(session_key, sizeof(session_key));
	uint8_t mac_key[64] = {0};
	NTSTATUS status;

	status = calculate_mac_key(&key, &samr_aes256_mac_key_salt, mac_key);
	assert_true(NT_STATUS_IS_OK(status));

	assert_memory_equal(expected_mac_key, mac_key, sizeof(mac_key));
}

static void torture_encrypt(void **state)
{
	NTSTATUS status;
	TALLOC_CTX *frame = talloc_stackframe();
	DATA_BLOB cek = {
		.data = discard_const_p(uint8_t, session_key),
		.length = sizeof(session_key),
	};
	const DATA_BLOB plaintext = {
		.data = discard_const_p(uint8_t, plaintext_data),
		.length = sizeof(plaintext_data),
	};
	DATA_BLOB iv = {
		.data = discard_const_p(uint8_t, salt_data),
		.length = sizeof(salt_data),
	};
	DATA_BLOB ctext;
	uint8_t auth_tag[64] = {0};

	assert_int_equal(iv.length, 16);

	status = samba_gnutls_aead_aes_256_cbc_hmac_sha512_encrypt(
		frame,
		&plaintext,
		&cek,
		&samr_aes256_enc_key_salt,
		&samr_aes256_mac_key_salt,
		&iv,
		&ctext,
		auth_tag);
	assert_true(NT_STATUS_IS_OK(status));
	assert_int_equal(ctext.length, 528);
	assert_non_null(ctext.data);

	assert_memory_equal(expected_auth_tag, auth_tag, sizeof(auth_tag));

	TALLOC_FREE(frame);
}

static void torture_encrypt_decrypt(void **state)
{
	NTSTATUS status;
	TALLOC_CTX *frame = talloc_stackframe();
	DATA_BLOB cek = data_blob_const(session_key, sizeof(session_key));
	const DATA_BLOB plaintext =
		data_blob_const(plaintext_data, sizeof(plaintext_data));
	DATA_BLOB iv = data_blob_const(salt_data, sizeof(salt_data));
	DATA_BLOB cipher_text;
	uint8_t auth_tag[64] = {0};
	DATA_BLOB plaintext_decrypted;

	status = samba_gnutls_aead_aes_256_cbc_hmac_sha512_encrypt(
		frame,
		&plaintext,
		&cek,
		&samr_aes256_enc_key_salt,
		&samr_aes256_mac_key_salt,
		&iv,
		&cipher_text,
		auth_tag);
	assert_true(NT_STATUS_IS_OK(status));
	assert_int_equal(cipher_text.length, 528);
	assert_in_range(cipher_text.length,
			plaintext.length,
			plaintext.length + 16);
	assert_non_null(cipher_text.data);

	status = samba_gnutls_aead_aes_256_cbc_hmac_sha512_decrypt(
		frame,
		&cipher_text,
		&cek,
		&samr_aes256_enc_key_salt,
		&samr_aes256_mac_key_salt,
		&iv,
		auth_tag,
		&plaintext_decrypted);
	assert_true(NT_STATUS_IS_OK(status));
	assert_non_null(plaintext_decrypted.data);
	assert_int_equal(plaintext_decrypted.length, plaintext.length);
	assert_memory_equal(plaintext_decrypted.data,
			    plaintext.data,
			    plaintext.length);

	TALLOC_FREE(frame);
}

#ifdef HAVE_GNUTLS_PBKDF2
/* The following hexdumps are from a Windows Server 2022 time trace */
static uint8_t pbkdf2_nt_hash[] = {
	0xf8, 0x48, 0x54, 0xde, 0xb8, 0x36, 0x10, 0x33,
	0xca, 0xea, 0x5c, 0x95, 0x96, 0x66, 0x99, 0x38
};

static uint8_t pbkdf2_iv[] = {
	0xd5, 0xbe, 0x4f, 0xd7, 0xb6, 0x85, 0xd1, 0xea,
	0xfd, 0x3b, 0xf4, 0x29, 0x83, 0xce, 0x10, 0x44
};

static uint8_t expected_pbkdf2_derived_key[] = {
	0xf1, 0xe6, 0xb2, 0x6a, 0x78, 0x28, 0x63, 0x05,
	0x77, 0x38, 0xc9, 0x71, 0xd2, 0x05, 0x88, 0x58
};

static void torture_pbkdf2(void **state)
{
	gnutls_datum_t nt_key = {
		.data = pbkdf2_nt_hash,
		.size = sizeof(pbkdf2_nt_hash),
	};
	gnutls_datum_t iv_datum = {
		.data = pbkdf2_iv,
		.size = sizeof(pbkdf2_iv),
	};
	uint64_t pbkdf2_iterations = 23533;
	uint8_t derived_key[16] = {0};
	int rc;

	rc = gnutls_pbkdf2(GNUTLS_MAC_SHA512,
			   &nt_key,
			   &iv_datum,
			   pbkdf2_iterations,
			   derived_key,
			   sizeof(derived_key));
	assert_int_equal(rc, 0);
	assert_memory_equal(derived_key,
			    expected_pbkdf2_derived_key,
			    sizeof(derived_key));
}
#endif /* HAVE_GNUTLS_PBKDF2 */

int main(int argc, char *argv[])
{
	int rc;
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(torture_enc_key),
		cmocka_unit_test(torture_mac_key),
		cmocka_unit_test(torture_encrypt),
		cmocka_unit_test(torture_encrypt_decrypt),
#ifdef HAVE_GNUTLS_PBKDF2
		cmocka_unit_test(torture_pbkdf2),
#endif /* HAVE_GNUTLS_PBKDF2 */
	};

	if (argc == 2) {
		cmocka_set_test_filter(argv[1]);
	}
	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);

	rc = cmocka_run_group_tests(tests, NULL, NULL);

	return rc;
}
